<?php

declare (strict_types=1);
namespace App\Model\admin;

use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property int $role_id 角色ID
 * @property int $permission_id 权限ID
 */
class SystemRolePermission extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'system_role_permission';

    public $timestamps = false;

    protected $primaryKey = 'id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['id', 'role_id', 'permission_id'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'role_id' => 'integer', 'permission_id' => 'integer'];

    /**
     * 添加权限
     * @param $role_id
     * @param $auth_ids
     * @return bool
     */
    public function addAuth($role_id,$auth_ids){
        if ($role_id==1) return true;

        if (!$auth_ids)
            return false;

        self::where('role_id',$role_id)->delete();

        foreach ($auth_ids as $auth_id) {
            self::create(['role_id'=>$role_id,'permission_id'=>$auth_id]);
        }
        return true;
    }

    /**
     * 获取权限id集
     * @param $role_ids
     * @return array
     */
    public static function getAuthByRoleId($role_ids): array
    {
        return self::query()->whereIn('role_id',$role_ids)->pluck('permission_id')->toArray();
    }
}